/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

import com.google.common.util.concurrent.Callables;
import entities.Album;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.spi.DirStateFactory;

/**
 *
 * @author tanlong
 */
public class AlbumDB {
    private String url;
    private String user;
    private String password;

    public AlbumDB(String url, String user, String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }

    public AlbumDB() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    public List<Album> showAlbum(){
        List<Album> list = new ArrayList<>();
        try {
            Connection conn = ConnectionUtil.getConnection(url, user, password);
            CallableStatement cstmt = conn.prepareCall("{call showAlbum}");
            ResultSet rs = cstmt.executeQuery();
            while(rs.next()){
                int albumid = rs.getInt(1);
                String albumname = rs.getString(2);
                String desciption = rs.getString(3);
                String datecreate = rs.getString(4);
                String username = rs.getString(5);
                Album a = new Album(albumid, albumname, desciption, datecreate, username);
                list.add(a);
            }
        } catch (SQLException ex) {
            Logger.getLogger(AlbumDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }
    
    public boolean insertAlbum(Album a){
        try {
           Connection conn = ConnectionUtil.getConnection(url, user, password);
             CallableStatement cstmt = conn.prepareCall("{call createAlbum (?,?,?,?)}");
            cstmt.setString(1, a.getAlbumname());
            cstmt.setString(2, a.getDescription());
            cstmt.setString(3, a.getDatecreate());
            cstmt.setString(4, a.getUsername());
            int rows = cstmt.executeUpdate();
            conn.close();
            return rows >0;
            
        } catch (Exception e) {
        }
        return false;
    }
    
    public boolean deleteAlbum(int albumid){
        try {
            Connection conn = ConnectionUtil.getConnection(url, user, password);
            CallableStatement cstmt = conn.prepareCall("{call deleteAlbum (?)}");
            cstmt.setInt(1, albumid);
            int rows = cstmt.executeUpdate();
            conn.close();
            return rows >0;
        } catch (Exception e) {
        }
        
        return false;
        
}
    
    public boolean updateAlbum(Album a){
        try {
            Connection conn = ConnectionUtil.getConnection(url, user, password);
            CallableStatement cstmt = conn.prepareCall("{call updateAlbum (?) }");
            cstmt.setInt("albumid",a.getAlbumid());
            int row = cstmt.executeUpdate();
            if(row>0){
                return true;
            }
        } catch (Exception e) {
        }
        
        return false;
    
    }
    
    public Album getAlbum(int albumid){
        try {
            Connection conn = ConnectionUtil.getConnection(url, user, password);
            CallableStatement cstmt = conn.prepareCall("{call getAlbum (?) }");
            cstmt.setInt("albumid", albumid);
            ResultSet rs = cstmt.executeQuery();
            if(rs.next()){
            String albumname = rs.getString(2);
            String description = rs.getString(3);
            String datecreate = rs.getString(4);
            String username = rs.getString(5);
            
            Album a = new Album(albumname, description, datecreate, username);
            return a;
            }
        } catch (Exception e) {
        }
        
        return null;
    
    }
}
